<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10 0010
 * Time: 上午 10:01
 */
namespace app\core\controller;
use app\admin\controller\Admin;
use think\Db;
class News extends Admin{

    //当前模块基础信息
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '新闻资讯管理',
                'description' => '管理新闻资讯',
            ),
            'menu' => array(
                array(
                    'name' => '新闻分类列表',
                    'url' => url('classList')
                ),
                array(
                    'name' => '新闻资讯列表',
                    'url' => url('newsList')
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加资讯内容',
                    'url' => url('newsAdd'),
                ),
                array(
                    'name' => '添加新闻分类',
                    'url' => url('classAdd'),
                ),
            ),
        );
    }

    //新闻分类列表
    public function classList(){
        $list = Db::name('core_news_classify')->where($this->class_condition(input()))->order('id desc')->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //新闻分类添加、修改
    public function classAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            if($data['id']){
                $content = '新闻分类修改';$status = 2;
                Db::name('core_news_classify')->where('id',$data['id'])->update($data);
            }else{
                $content = '新闻分类添加';$status = 1;
                if(Db::name('core_news_classify')->where('class_name',$data['class_name'])->find()){
                    return ajaxReturn(500,'新闻分类名称存在');
                }
                $data['create_time'] = time();
                Db::name('core_news_classify')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('classList'));
        }
        $this->view->info = Db::name('core_news')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //新闻分类删除
    public function classDel(){
        $content = '新闻资讯删除';$status = 3;
        if(Db::name('core_news')->where('class_id',input('id'))->find()){
            return ajaxReturn(500,'当前分类下存在新闻资讯，请删除后操作');
        }else{
            Db::name('core_news_classify')->delete(input('id'));
        }
        $this->log($content,$status);
        return ajaxReturn(200,'删除成功');
    }

    //新闻资讯列表
    public function newsList(){
        $list = Db::name('core_news')->order('id desc')->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //新闻资讯添加、修改
    public function newsAdd(){
        if($this->request->isPost()){
            $data = input('post.');
//            if($data['recommend_position'] == 1){
//                if($data['recommend'] == 2){
//                    return ajaxReturn(500,'请先设置推荐');
//                }
//                if(Db::name('core_news')->where(['type'=>1,'recommend_position'=>1])->count() >= 4){
//                    return ajaxReturn(500,'图集只能推荐四个');
//                }
//            }
            if($data['id']){
                $content = '新闻资讯修改';$status = 2;
                Db::name('core_news')->where('id',$data['id'])->update($data);
            }else{
                $content = '新闻资讯添加';$status = 1;
                $data['create_time'] = time();
                Db::name('core_news')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('newsList'));
        }
        $this->view->info = Db::name('core_news')->where('id',input('id'))->find();
        $this->view->classInfo = Db::name('core_news_classify')->where('status',1)->select();
        return $this->fetch();
    }

    //内容删除
    public function del(){
        $content = '新闻资讯删除';$status = 3;
        if($image = Db::name('core_news')->where('id',input('id'))->value('image')){
            if(!unlink(ROOT_PATH.'/public'.$image)) {
                return ajaxReturn('500','内置错误，请尽快联系管理员！');
            }
        }
        $this->log($content,$status);
        Db::name('core_news')->delete(input('id'));
        return ajaxReturn('200');
    }

    protected function class_condition($data){
        $where = '';
        if(!empty($data['class_name'])){
            $where['class_name'] = array('like','%'.$data['class_name'].'%');
        }
        if(!empty($data['status'])){
            $where['status'] = $data['status'];
        }
        return $where;
    }
}